package com.zzt.questionbankquestion.service.impl;

import com.zzt.questionbankquestion.bean.QuestionType;
import com.zzt.questionbankquestion.bean.dto.PageResult;
import com.zzt.questionbankquestion.bean.dto.QuestionTypeDTO;
import com.zzt.questionbankquestion.bean.dto.Result;
import com.zzt.questionbankquestion.mapper.QuestionTypeConverter;
import com.zzt.questionbankquestion.repository.QuestionTypeRepository;
import com.zzt.questionbankquestion.service.QuestionTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class QuestionTypeServiceImpl implements QuestionTypeService {

    @Autowired
    private QuestionTypeConverter questionTypeConverter;

    @Autowired
    private QuestionTypeRepository questionTypeRepository;

    @Override
    public PageResult< QuestionTypeDTO > getAPage(PageRequest pageRequest) {
        Page<QuestionType> page=questionTypeRepository.findAll(pageRequest);
        List<QuestionType> dos=page.getContent();
        List<QuestionTypeDTO> dtos=questionTypeConverter.doToDto(dos);
        PageResult pageResult=new PageResult();
        pageResult.setCount(page.getTotalElements());
        pageResult.setPage(page.getTotalPages());
        pageResult.setResult(dtos);
        return pageResult;
    }

    @Override
    public PageResult< QuestionTypeDTO > getASpecialPage(Specification< QuestionType > specification, PageRequest pageRequest) {
        Page<QuestionType> page=questionTypeRepository.findAll(specification,pageRequest);
        List<QuestionType> dos=page.getContent();
        List<QuestionTypeDTO> dtos=questionTypeConverter.doToDto(dos);
        PageResult pageResult=new PageResult();
        pageResult.setCount(page.getTotalElements());
        pageResult.setPage(page.getTotalPages());
        pageResult.setResult(dtos);
        return pageResult;
    }
}
